import type { WorkOrderModel } from './models';

import { requestClient } from '#/api/request';

/**
 * 获取工单数据集合
 * @param params 查询参数
 * @returns 返回查询结果
 */
export async function getWorkOrderListApi(params?: any) {
  return requestClient.get<WorkOrderModel[]>('/mes/production/workorder/list', {
    params,
  });
}

/**
 * 分页查询工单数据集合
 * @param params 分页查询参数
 * @returns 返回查询结果
 */
export async function getWorkOrderPagedListApi(params?: PageParams) {
  return requestClient.get<PaginationResult<WorkOrderModel>>(
    '/mes/production/workorder/list-page',
    {
      params,
    },
  );
}

/**
 * 获取排产工单数据集合
 * @param lineId 产线 Id
 * @returns 返回查询结果
 */
export async function getScheduleWorkOrderListApi(lineId: number) {
  return requestClient.get<WorkOrderModel[]>(
    '/mes/production/workorder/schedule/list',
    {
      params: { lineId },
    },
  );
}

/**
 * 分页查询生产系列数据
 * @param params 分页查询参数
 * @returns 返回查询结果
 */
export async function getSequenceWorkOrderPagedListApi(params?: PageParams) {
  return requestClient.get<PaginationResult<WorkOrderModel>>(
    '/mes/production/workorder/sequence/list-page',
    {
      params,
    },
  );
}

/**
 * 创建工单
 * @param data 要创建的工单
 * @returns 返回结果
 */
export async function createWorkOrderApi(data: any) {
  return requestClient.post<boolean>('/mes/production/workorder', data);
}

/**
 * 更新工单
 * @param id 工单 Id
 * @param data 要更新的工单
 * @returns 返回结果
 */
export async function updateWorkOrderApi(id: number, data: any) {
  return requestClient.put<boolean>(`/mes/production/workorder/${id}`, data);
}

/**
 * 下发工单
 * @param id 工单 Id
 * @returns 返回结果
 */
export async function issueWorkOrderApi(id: number) {
  return requestClient.put<boolean>(`/mes/production/workorder/issue/${id}`);
}

/**
 * 工单排程
 * @param id 工单 Id
 * @returns 返回结果
 */
export async function scheduleWorkOrderApi(id: number) {
  return requestClient.put<boolean>(`/mes/production/workorder/schedule/${id}`);
}

/**
 * 工单生产排序
 * @param fromWoId 来源工单 Id
 * @param toWoId 目标工单 Id
 * @returns 返回结果
 */
export async function sortWorkOrderApi(fromWoId: number, toWoId: number) {
  return requestClient.put<boolean>('/mes/production/workorder/sort/', {
    fromWoId,
    toWoId,
  });
}

/**
 * 暂停/恢复工单
 * @param id 工单 Id
 * @returns 返回结果
 */
export async function holdOnWorkOrderApi(id: number) {
  return requestClient.put<boolean>(`/mes/production/workorder/holdOn/${id}`);
}

/**
 * 取消工单
 * @param id 工单 Id
 * @returns 返回结果
 */
export async function cancelWorkOrderApi(id: number) {
  return requestClient.put<boolean>(`/mes/production/workorder/cancel/${id}`);
}

/**
 * 关闭工单
 * @param id 工单 Id
 * @returns 返回结果
 */
export async function closeWorkOrderApi(id: number) {
  return requestClient.put<boolean>(`/mes/production/workorder/close/${id}`);
}
